package com.shopnight.controller.chatRoom;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopnight.entity.ChatPhiz;
import com.shopnight.interceptors.JWTInterceptor;
import com.shopnight.result.ApiResult;
import com.shopnight.result.ApiResultList;
import com.shopnight.service.ChatPhizService;
import com.shopnight.service.ChatRoomInferiorService;
import com.shopnight.socketVo.MessageVo;
import com.shopnight.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = {"WebSocketController"},description = "聊天接口")
@RestController
@RequestMapping("/api")
public class WebSocketController {

    @Resource
    private ChatRoomInferiorService chatRoomInferiorService;
    @Autowired
    private ChatPhizService chatPhizService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/token/signRead")
    @ApiOperation(value = "清除帖子未读消息",notes = "")
    public ApiResult signRead(@RequestParam Integer postId){
        String userId = UserContext.getEndUserId();
        //清除此贴未读消息
        stringRedisTemplate.opsForHash().put("mqtt-topic:" + userId ,"wall-post:" + postId, "0");
        return ApiResult.ok(null);
    }


    @GetMapping("/getMessageList")
    @ApiOperation(value = "获取消息列表",notes = "")
    public ApiResult getMessageList(@RequestParam Map<String, String> params){
        Page<MessageVo> page = chatRoomInferiorService.pageMessage(params);
        return ApiResult.ok(page);
    }

    @GetMapping("/token/getChatPhizList")
    @ApiOperation(value = "获取用户表情图列表",notes = "")
    public ApiResultList getChatPhizList(){
        Integer userId =  Integer.valueOf(UserContext.getEndUserId());
        LambdaQueryWrapper<ChatPhiz> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatPhiz::getUserId,userId);

        return ApiResultList.ok(  chatPhizService.list(wrapper) );
    }

    @PostMapping("/token/addChatPhiz")
    @ApiOperation(value = "新增表情图",notes = "传url")
    public ApiResult addChatPhiz(@RequestBody Map<String,Object> map){
        Integer userId =  Integer.valueOf(UserContext.getEndUserId());
        ChatPhiz chatPhiz = new ChatPhiz();
        chatPhiz.setUserId(userId);
        chatPhiz.setPhizUrl(map.get("url").toString());
        return ApiResult.ok(  chatPhizService.save(chatPhiz) );
    }

    @GetMapping("/token/delChatPhiz")
    @ApiOperation(value = "删除表情图",notes = "传id")
    public ApiResult delChatPhiz(@RequestParam Integer id){
        Integer userId =  Integer.valueOf(UserContext.getEndUserId());
        LambdaQueryWrapper<ChatPhiz> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatPhiz::getUserId,userId)
               .eq(ChatPhiz::getId,id);

        return ApiResult.ok(   chatPhizService.remove(wrapper) );
    }


}
